package operatation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-03-23
 * Time: 20:32
 */
public class Delete implements IOperatation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择你要删除的书：");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        int delnum = -1;
        int i = 0;
        for (; i < currentSize ; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                delnum = i;
                break;
            }
        }
        if(i == currentSize) {
            System.out.println("没有这本书");
            return;
        }
        for (int j = delnum; j < bookList.getUsedSize() - 1; j++) {
            Book book= bookList.getBook(j + 1);
            bookList.setBooks(book,j);
        }
        bookList.setBooks(null,currentSize - 1);
        bookList.setUsedSize(currentSize - 1);


    }

}
